home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / emacs / emacs1857 / src_d2.zoo / source / print.c < prev    next >
C/C++ Source or Header  |  1991-12-02  |  18KB  |  699 lines

  1. /* Lisp object printing and output streams.
  2.    Copyright (C) 1985, 1986, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23. #undef NULL
  24. #include "lisp.h"
  25.  
  26. #ifndef standalone
  27. #include "buffer.h"
  28. #include "window.h"
  29. #include "process.h"
  30. #include "dispextern.h"
  31. #include "termchar.h"
  32. #endif /* not standalone */
  33.  
  34. Lisp_Object Vstandard_output, Qstandard_output;
  35.  
  36. /* Avoid actual stack overflow in print.  */
  37. int print_depth;
  38.  
  39. /* Maximum length of list to print in full; noninteger means
  40.    effectively infinity */
  41.  
  42. Lisp_Object Vprint_length;
  43.  
  44. /* Nonzero means print newlines in strings as \n.  */
  45.  
  46. int print_escape_newlines;
  47.  
  48. /* Nonzero means print newline before next minibuffer message.
  49.    Defined in xdisp.c */
  50.  
  51. extern int noninteractive_need_newline;
  52. #ifdef MAX_PRINT_CHARS
  53. static int print_chars;
  54. static int max_print;
  55. #endif /* MAX_PRINT_CHARS */
  56.  
  57. /* Low level output routines for charaters and strings */
  58.  
  59. /* Lisp functions to do output using a stream
  60.  must have the stream in a variable called printcharfun
  61.  and must start with PRINTPREPARE and end with PRINTFINISH.
  62.  Use PRINTCHAR to output one character,
  63.  or call strout to output a block of characters.
  64.  Also, each one must have the declarations
  65.    struct buffer *old = current_buffer;
  66.    int old_point = -1, start_point;
  67.    Lisp_Object original;
  68. */ 
  69.  
  70. #define PRINTPREPARE \
  71.    original = printcharfun; \
  72.    if (NULL (printcharfun)) printcharfun = Qt; \
  73.    if (XTYPE (printcharfun) == Lisp_Buffer) \
  74.      { if (XBUFFER (printcharfun) != current_buffer) Fset_buffer (printcharfun); \
  75.        printcharfun = Qnil;}\
  76.    if (XTYPE (printcharfun) == Lisp_Marker) \
  77.      { if (XMARKER (original)->buffer != current_buffer) \
  78.          set_buffer_internal (XMARKER (original)->buffer); \
  79.        old_point = point; \
  80.        SET_PT (marker_position (printcharfun)); \
  81.        start_point = point; \
  82.        printcharfun = Qnil;}
  83.  
  84. #define PRINTFINISH \
  85.    if (XTYPE (original) == Lisp_Marker) \
  86.      Fset_marker (original, make_number (point), Qnil); \
  87.    if (old_point >= 0) \
  88.      SET_PT ((old_point >= start_point ? point - start_point : 0) + old_point); \
  89.    if (old != current_buffer) \
  90.      set_buffer_internal (old)
  91.  
  92. #define PRINTCHAR(ch) printchar (ch, printcharfun)
  93.  
  94. /* Index of first unused element of message_buf.  */
  95. static int printbufidx;
  96.  
  97. static void
  98. printchar (ch, fun)
  99.      unsigned char ch;
  100.      Lisp_Object fun;
  101. {
  102.   Lisp_Object ch1;
  103.  
  104. #ifdef MAX_PRINT_CHARS
  105.   if (max_print)
  106.     print_chars++;
  107. #endif /* MAX_PRINT_CHARS */
  108. #ifndef standalone
  109.   if (EQ (fun, Qnil))
  110.     {
  111.       QUIT;
  112.       insert (&ch, 1);
  113.       return;
  114.     }
  115.   if (EQ (fun, Qt))
  116.     {
  117.       if (noninteractive)
  118.     {
  119.       putchar (ch);
  120.       noninteractive_need_newline = 1;
  121.       return;
  122.     }
  123.       if (echo_area_contents != message_buf)
  124.     echo_area_contents = message_buf, printbufidx = 0;
  125.       if (printbufidx < screen_width)
  126.     message_buf[printbufidx++] = ch;
  127.       message_buf[printbufidx] = 0;
  128.       return;
  129.     }
  130. #endif /* not standalone */
  131.  
  132.   XFASTINT (ch1) = ch;
  133.   call1 (fun, ch1);
  134. }
  135.  
  136. static void
  137. strout (ptr, size, printcharfun)
  138.      char *ptr;
  139.      int size;
  140.      Lisp_Object printcharfun;
  141. {
  142.   int i = 0;
  143.  
  144.   if (EQ (printcharfun, Qnil))
  145.     {
  146.       insert (ptr, size >= 0 ? size : strlen (ptr));
  147. #ifdef MAX_PRINT_CHARS
  148.       if (max_print)
  149.         print_chars += size >= 0 ? size : strlen(ptr);
  150. #endif /* MAX_PRINT_CHARS */
  151.       return;
  152.     }
  153.   if (EQ (printcharfun, Qt))
  154.     {
  155.       i = size >= 0 ? size : strlen (ptr);
  156. #ifdef MAX_PRINT_CHARS
  157.       if (max_print)
  158.         print_chars += i;
  159. #endif /* MAX_PRINT_CHARS */
  160.       if (noninteractive)
  161.     {
  162.       fwrite (ptr, 1, i, stdout);
  163.       noninteractive_need_newline = 1;
  164.       return;
  165.     }
  166.       if (echo_area_contents != message_buf)
  167.     echo_area_contents = message_buf, printbufidx = 0;
  168.       if (i > screen_width - printbufidx)
  169.     i = screen_width - printbufidx;
  170.       bcopy (ptr, &message_buf[printbufidx], i);
  171.       printbufidx += i;
  172.       message_buf[printbufidx] = 0;
  173.       return;
  174.     }
  175.   if (size >= 0)
  176.     while (i < size)
  177.       PRINTCHAR (ptr[i++]);
  178.   else
  179.     while (ptr[i])
  180.       PRINTCHAR (ptr[i++]);
  181. }
  182.  
  183. DEFUN ("write-char", Fwrite_char, Swrite_char, 1, 2, 0,
  184.   "Output character CHAR to stream STREAM.\n\
  185. STREAM defaults to the value of `standard-output' (which see).")
  186.   (ch, printcharfun)
  187.      Lisp_Object ch, printcharfun;
  188. {
  189.   struct buffer *old = current_buffer;
  190.   int old_point = -1;
  191.   int start_point;
  192.   Lisp_Object original;
  193.  
  194.   if (NULL (printcharfun))
  195.     printcharfun = Vstandard_output;
  196.   CHECK_NUMBER (ch, 0);
  197.   PRINTPREPARE;
  198.   PRINTCHAR (XINT (ch));
  199.   PRINTFINISH;
  200.   return ch;
  201. }
  202.  
  203. write_string (data, size)
  204.      char *data;
  205.      int size;
  206. {
  207.   struct buffer *old = current_buffer;
  208.   Lisp_Object printcharfun;
  209.   int old_point = -1;
  210.   int start_point;
  211.   Lisp_Object original;
  212.  
  213.   printcharfun = Vstandard_output;
  214.  
  215.   PRINTPREPARE;
  216.   strout (data, size, printcharfun);
  217.   PRINTFINISH;
  218. }
  219.  
  220. write_string_1 (data, size, printcharfun)
  221.      char *data;
  222.      int size;
  223.      Lisp_Object printcharfun;
  224. {
  225.   struct buffer *old = current_buffer;
  226.   int old_point = -1;
  227.   int start_point;
  228.   Lisp_Object original;
  229.  
  230.   PRINTPREPARE;
  231.   strout (data, size, printcharfun);
  232.   PRINTFINISH;
  233. }
  234.  
  235.  
  236. #ifndef standalone
  237.  
  238. temp_output_buffer_setup (bufname)
  239.     char *bufname;
  240. {
  241.   register struct buffer *old = current_buffer;
  242.   register Lisp_Object buf;
  243.  
  244.   Fset_buffer (Fget_buffer_create (build_string (bufname)));
  245.  
  246.   current_buffer->read_only = Qnil;
  247.   Ferase_buffer ();
  248.  
  249.   XSET (buf, Lisp_Buffer, current_buffer);
  250.   specbind (Qstandard_output, buf);
  251.  
  252.   set_buffer_internal (old);
  253. }
  254.  
  255. Lisp_Object
  256. internal_with_output_to_temp_buffer (bufname, function, args)
  257.      char *bufname;
  258.      Lisp_Object (*function) ();
  259.      Lisp_Object args;
  260. {
  261.   int count = specpdl_ptr - specpdl;
  262.   Lisp_Object buf, val;
  263.  
  264.   record_unwind_protect (Fset_buffer, Fcurrent_buffer ());
  265.   temp_output_buffer_setup (bufname);
  266.   buf = Vstandard_output;
  267.  
  268.   val = (*function) (args);
  269.  
  270.   temp_output_buffer_show (buf);
  271.  
  272.   unbind_to (count);
  273.   return val;
  274. }
  275.  
  276. DEFUN ("with-output-to-temp-buffer", Fwith_output_to_temp_buffer, Swith_output_to_temp_buffer,
  277.        1, UNEVALLED, 0,
  278.   "Binding `standard-output' to buffer named BUFNAME, execute BODY then display that buffer.\n\
  279. The buffer is cleared out initially, and marked as unmodified when done.\n\
  280. All output done by BODY is inserted in that buffer by default.\n\
  281. It is displayed in another window, but not selected.\n\
  282. The value of the last form in BODY is returned.\n\
  283. If variable `temp-buffer-show-hook' is non-nil, call it at the end\n\
  284. to get the buffer displayed.  It gets one argument, the buffer to display.")
  285.   (args)
  286.      Lisp_Object args;
  287. {
  288.   struct gcpro gcpro1;
  289.   Lisp_Object name;
  290.   int count = specpdl_ptr - specpdl;
  291.   Lisp_Object buf, val;
  292.  
  293.   GCPRO1(args);
  294.   name = Feval (Fcar (args));
  295.   UNGCPRO;
  296.  
  297.   CHECK_STRING (name, 0);
  298.   temp_output_buffer_setup (XSTRING (name)->data);
  299.   buf = Vstandard_output;
  300.  
  301.   val = Fprogn (Fcdr (args));
  302.  
  303.   temp_output_buffer_show (buf);
  304.  
  305.   unbind_to (count);
  306.   return val;
  307. }
  308. #endif /* not standalone */
  309.  
  310. static void print ();
  311.  
  312. DEFUN ("terpri", Fterpri, Sterpri, 0, 1, 0,
  313.   "Output a newline to STREAM (or value of standard-output).")
  314.   (printcharfun)
  315.      Lisp_Object printcharfun;
  316. {
  317.   struct buffer *old = current_buffer;
  318.   int old_point = -1;
  319.   int start_point;
  320.   Lisp_Object original;
  321.  
  322.   if (NULL (printcharfun))
  323.     printcharfun = Vstandard_output;
  324.   PRINTPREPARE;
  325.   PRINTCHAR ('\n');
  326.   PRINTFINISH;
  327.   return Qt;
  328. }
  329.  
  330. DEFUN ("prin1", Fprin1, Sprin1, 1, 2